package geektime.spring.springbuks.controller;

import geektime.spring.springbuks.bean.dp1.Account;
import geektime.spring.springbuks.service.db1.AccountService;
import geektime.spring.springbuks.util.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("demo")
public class CurdController {
    @Autowired
    private AccountService accountService;
    /**
     * 鏌ヨ鏁版嵁
     */
    @PostMapping("/getAccount")
    public ResultBody selectAccount(Long userId,
                                     @RequestParam(defaultValue = "1")int pageSize,
                                     @RequestParam(defaultValue = "10")int page){
           PageInfo<Account> accounts=accountService.selectAccount(userId,page,pageSize);
           System.out.println(accounts);
           if (accounts.getSize()<10){

               return ResultBody.success(accounts,0);

           }else {
               return ResultBody.success(accounts,1);
           }

    }

    /**
     * 鏂板鏁版嵁
     */
    @Transactional(transactionManager="db1TransactionManager")
    @RequestMapping(value = "/insertAccount",method = RequestMethod.POST)
    public ResultBody insertAccount(Account account){
        int insertStatus=accountService.insertAccount(account);
        if (insertStatus!=1){
            return ResultBody.error("鏂板澶辫触");
        }
        return  ResultBody.success(insertStatus,1);
    }
    /**
     * 淇敼鏁版嵁
     */
    @Transactional(transactionManager="db1TransactionManager")
    @RequestMapping(value = "/insertAccount",method = RequestMethod.POST)
    public ResultBody UpdateAccount(Account account){
        int insertStatus=accountService.UpdateAccount(account);
        if (insertStatus!=1){
            return ResultBody.error("淇敼澶辫触");
        }
        return  ResultBody.success(insertStatus,1);
    }
}
